const e = require('express');
let db=require('../db/index')

// 业主报修列表查询
exports.warrantylists = (req, res) => {
    var sql =
        `SELECT * FROM warranty`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
//业主报修分配处理
exports.warrantyupdate = (req, res) => {
    var sql =
        `UPDATE warranty set Processingpersonnel='${req.body.region}',processingtime='2021/05/31 10:01:45',state='处理中',edit='' WHERE Warranty_id=${req.body.patchid}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM warranty`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 业主报修数据删除
exports.warrantydelete = (req, res) => {
    var sql =
        `delete from warranty WHERE Warranty_id=${req.body.patchid}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM warranty`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};





// 投诉建议

exports.complaintlists = (req, res) => {
    var sql =
        `SELECT * FROM complaint`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
//投诉建议分配处理
exports.complaintupdate = (req, res) => {
    var sql =
        `UPDATE complaint set Processingpersonnel='${req.body.region}',processingtime='2021/05/31 10:01:45',state='处理中',edit='' WHERE complain_id=${req.body.patchid}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM complaint`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};

// 投诉建议数据删除
exports.complaintdelete = (req, res) => {
    var sql =
        `delete from complaint WHERE complain_id=${req.body.patchid}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM complaint`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};


// 内部工单查询
exports.workorderlists = (req, res) => {
    var sql =
        `SELECT * FROM workorder`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 内部工单删除
exports.workorderdelete = (req, res) => {
    var sql =
        `delete from workorder WHERE workorder_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM workorder`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 添加工单
exports.workorderadd = (req, res) => {
    if(req.body.workordertype=='公共报修'){
        var sql = 
        `INSERT INTO warranty(Belongingcommunity,Typeofrepairreport,Reportedrepaircommunity,Repairer,Repairtime,state,edit,del,view)VALUES('${req.body.Belongingcommunity}','${req.body.Complainttype}','1号楼1单元2032','赵小刚','2024-12-17 11:05:32','未处理','分配','删除','查看')`
    }else{
        var sql =
        `INSERT INTO complaint(Belongingcommunity,Complainttype,Complaintcommunity,complainant,Complainttime,state,edit,del,view)VALUES('${req.body.Belongingcommunity}','${req.body.Complainttype}','1号楼1单元2032','赵小刚','2021-05-31 10:01:45','未处理',' 分配',' 删除','查看')`;
    }
    
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `INSERT INTO workorder(Belongingcommunity,Typeofrepairreport,Reportedrepaircommunity,Repairer,Repairtime,state,edit,del,view)VALUES('${req.body.Belongingcommunity}','${req.body.Complainttype}','1号楼1单元2032','赵小刚','2021-05-31 10:01:45','未处理','分配','删除',' 查看')`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    var sql =
                        `SELECT * FROM workorder`;
                    db.query(sql, [], (err, data) => {
                        if (err) {
                            console.log("出错了", err);
                            return res.send("错误" + err.message);
                        }else{
                            res.send(data);
                        }
                    });
                }
            });
        }
    });
};


// 访客邀请
exports.visitorlists = (req, res) => {
    var sql =
        `SELECT * FROM visitor`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};

// 装修申请
exports.decorationlists = (req, res) => {
    var sql =
        `SELECT * FROM decoration`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
    });
};
// 装修申请交押金
exports.decorationdeposit = (req, res) => {
    var sql =
        `UPDATE decoration SET state='待验收',deposit='',checks ='装修验收' WHERE decora_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM decoration`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 装修申请删除
exports.decorationdelete = (req, res) => {
    var sql =
        `delete from decoration WHERE decora_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM decoration`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};
// 装修申请验收
exports.decorationcheck = (req, res) => {
    var sql =
        `UPDATE decoration SET state='已结案',deposit='',checks ='' WHERE decora_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                `SELECT * FROM decoration`;
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }else{
                    res.send(data);
                }
            });
        }
    });
};